﻿using Buddy.Coroutines;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Coroutines;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;



namespace Paster.Spell
{
    partial class Spells
    {

        public static async Task<bool> Cast(string Spell, bool reqs, WoWUnit target)
        {
            if (!reqs) return false;
            if (!SpellManager.HasSpell(Spell)) return false;
            if (!SpellManager.CanCast(Spell, target)) return false;
            if (!SpellManager.Cast(Spell, target)) return false;

            Logging.Write(Colors.Yellow, "Casting: " + Spell + " on: " + target.SafeName);
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
        public static async Task<bool> Cast(int Spell, bool reqs, WoWUnit target)
        {
            if (!reqs) return false;
            if (!SpellManager.HasSpell(Spell)) return false;
            if (!SpellManager.CanCast(Spell, target)) return false;
            if (!SpellManager.Cast(Spell, target)) return false;

            Logging.Write(Colors.Yellow, "Casting: " + WoWSpell.FromId(Spell).Name + " on: " + target.SafeName);
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }

        public static async Task<bool> CastBuff(string Spell, bool reqs, WoWUnit target)
        {
            if (!reqs) return false;
            if (!SpellManager.HasSpell(Spell)) return false;
            if (!SpellManager.CanCast(Spell, target)) return false;
            if (!SpellManager.Cast(Spell, target)) return false;

            Logging.Write(Colors.Yellow, "Casting: " + Spell + " on: " + target.SafeName);
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
        public static async Task<bool> CastBuff(int Spell, bool reqs, WoWUnit target)
        {
            if (!reqs) return false;
            if (!SpellManager.HasSpell(Spell)) return false;
            if (!SpellManager.CanCast(Spell, target)) return false;
            if (!SpellManager.Cast(Spell, target)) return false;

            Logging.Write(Colors.Yellow, "Casting: " + WoWSpell.FromId(Spell).Name + " on: " + target.SafeName);
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
        private static async Task<bool> CastGroundSpell(string spell, bool reqs, WoWUnit unit)
        {
            if (!reqs) return false;
            if (!SpellManager.CanCast(spell)) return false;
            if (!SpellManager.Cast(spell)) return false;

            if (!await Coroutine.Wait(1000, () => StyxWoW.Me.CurrentPendingCursorSpell != null))
            {
                Logging.WriteDiagnostic("Cursor didn't turn into the spell!");
                return false;
            }
            SpellManager.ClickRemoteLocation(unit.Location);

            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
        private static async Task<bool> CastGroundSpell(int spell, bool reqs, WoWUnit unit)
        {
            if (!reqs) return false;
            if (!SpellManager.CanCast(spell)) return false;
            if (!SpellManager.Cast(spell)) return false;

            if (!await Coroutine.Wait(1000, () => StyxWoW.Me.CurrentPendingCursorSpell != null))
            {
                Logging.WriteDiagnostic("Cursor didn't turn into the spell!");
                return false;
            }
            SpellManager.ClickRemoteLocation(unit.Location);

            Logging.Write(Colors.Yellow, "Casting: " + WoWSpell.FromId(spell).Name + " on: " + unit.SafeName);
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
        public static async Task<bool> UseItem(int itemID, bool reqs)
        {
            if (!reqs) return false;
            WoWItem potion = StyxWoW.Me.BagItems.FirstOrDefault(h => h.Entry == itemID);
            if (potion == null || potion.CooldownTimeLeft.TotalMilliseconds > 0)
            {
                return false;
            }
            potion.Use();
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
    }
}
